package com.share.thread;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @desc:演示CopyOnWriteArrayList迭代期间可以修改集合的内容
 * @author:caifan
 * @date:2022/4/11
 */
public class CopyOnWriteArrayListDemo01 {
    public static void main(String[] args) {
        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>(List.of(1, 2, 3));
        System.out.println(list); // [1, 2, 3]

        Iterator<Integer> it1 = list.iterator();
        list.add(5);

        System.out.println(list); // [1, 2, 3, 5]
        Iterator<Integer> it2 = list.iterator();

        it1.forEachRemaining(System.out::print);// 123
        System.out.println();
        it2.forEachRemaining(System.out::print);// 1235
    }
}
